package com.store;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jedi.BaseObj;
import com.tss.util.DbConn;
import com.tss.util.DbRs;

public class CompanySaleMgr extends BaseObj{
	
public Companysale getSaleInfo(int saleid){
		
		
		clearErr();
		Companysale sale = new Companysale();
		DbConn conn = new DbConn();
		try {
			String sql = "";
			sql = "select * from companysales where saleid = ?";
			conn.prepare(sql);
			conn.setInt(1,saleid);
			DbRs rs = conn.executeQuery();
			if (rs == null || rs.size() ==0) return null;
			sale.setComid(getInt(rs,0,"comid"));
			sale.setRemark(get(rs,0,"remark"));
			sale.setSaleid(getInt(rs,0,"saleid"));
			sale.setSalemail(get(rs,0,"salemail"));
			sale.setSalename(get(rs,0,"salename"));
			sale.setSaletel(get(rs,0,"saletel"));
			sale.setCreatetime(get(rs,0,"createtime"));
		
			return sale;
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
	}

	public List getSales(){
	
		
			clearErr();
			DbConn conn = new DbConn();
			DbRs rs = null;
			try {
				String sql = "";
				sql = "select * from companysales where 1=1";
				
				conn.prepare(sql);
				rs = conn.executeQuery();
			}
			catch(Exception ex) {
				ex.printStackTrace();
				setErr(ex.getMessage());
				return null;
			}
			finally {
				conn.close();
			}
			if (rs != null && rs.size() >0) {
				List r = new ArrayList();
				for(int i=0;i<rs.size();i++) {
					Companysale comsale = new Companysale(get(rs,i,"saleid"));
					comsale.setComid(getInt(rs,i,"comid"));
					comsale.setSalename(get(rs,i,"salename"));
					comsale.setSalemail(get(rs,i,"salemail"));
					//project(get(rs,i,"phone"));
					comsale.setSaletel(get(rs,i,"saletel"));
					comsale.setSaleid(getInt(rs,i,"saleid"));
					comsale.setCreatetime(get(rs,i,"createtime"));
				
					
					
					r.add(comsale);
				}
				return r;
			}
			return null;
		}
	public List getSalesByid(int comid){
		
		
		clearErr();
		DbConn conn = new DbConn();
		DbRs rs = null;
		try {
			String sql = "";
			sql = "select * from companysales where comid=?";
			
			conn.prepare(sql);
			conn.setInt(1,comid);
			rs = conn.executeQuery();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
		if (rs != null && rs.size() >0) {
			List r = new ArrayList();
			for(int i=0;i<rs.size();i++) {
				Companysale sale = new Companysale(getInt(rs,i,"comid"));
				sale.setComid(getInt(rs,i,"comid"));
				sale.setCreatetime(get(rs,i,"createtime"));
				sale.setSalemail(get(rs,i,"salemail"));
			
				sale.setSaletel(get(rs,i,"saletel"));
				sale.setSalename(get(rs,i,"salename"));
				sale.setRemark(get(rs,i,"remark"));
				sale.setSaleid(getInt(rs,i,"saleid"));
			r.add(sale);
			}
			return r;
		}
		return null;
	}
	public List getAllSales(){
		
		
		clearErr();
		DbConn conn = new DbConn();
		DbRs rs = null;
		try {
			String sql = "";
			sql = "select * from companysales where 1=1";
			
			conn.prepare(sql);
			
			rs = conn.executeQuery();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
		if (rs != null && rs.size() >0) {
			List r = new ArrayList();
			for(int i=0;i<rs.size();i++) {
				Companysale sale = new Companysale(getInt(rs,i,"comid"));
				sale.setComid(getInt(rs,i,"comid"));
				sale.setCreatetime(get(rs,i,"createtime"));
				sale.setSalemail(get(rs,i,"salemail"));
			
				sale.setSaletel(get(rs,i,"saletel"));
				sale.setSalename(get(rs,i,"salename"));
				sale.setRemark(get(rs,i,"remark"));
				sale.setSaleid(getInt(rs,i,"saleid"));
			r.add(sale);
			}
			return r;
		}
		return null;
	}
	public List get(String otherSQL, String orderField, int pageNo, int pageSize) {
		clearErr();
		DbRs rs = null;
		DbConn conn = null;
		try {
			conn = new DbConn();
			String sqlCount = "select count(*) as total from companysales a where 1=1 ";
			if (otherSQL != null && !otherSQL.trim().equals(""))
				sqlCount += otherSQL;
			conn.prepare(sqlCount);
			DbRs rsCount = conn.executeQuery();
			int total = getInt(rsCount, 0, "total");
			setTotal(total);
			boolean b = total % pageSize == 0;
			int pageCount = b ? (total / pageSize) : (total / pageSize + 1);
			if (pageNo > pageCount)
				pageNo = pageCount;
			// String sql = "select * from projectinfo where 1=1";
			String sql = "select a.*, substring(a.createtime, 1,10) as screatetime ,b.comname as comname_str "
					+ "from companysales a  Left Join companyinfo b On a.comid = b.comid Where 1=1 ";

			if (otherSQL != null && !otherSQL.trim().equals(""))
				sql += otherSQL;
			if (orderField != null && !orderField.trim().equals(""))
				sql += " order by " + orderField;
			conn.prepare(sql);
			rs = conn.executeQuery(pageNo, pageSize);
		} catch (SQLException ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return new ArrayList();
		} catch (Exception e) {
			e.printStackTrace();
			setErr(e.getMessage());
			return new ArrayList();
		} finally {
			conn.close();
		}
		List r = new ArrayList();
		if (rs == null || rs.size() <= 0)
			return r;
		for (int i = 0; i < rs.size(); i++) {
			Companysale comsale = new Companysale(get(rs, i, "saleid"));
			comsale.setComid(getInt(rs, i, "comid"));
			comsale.setComname_str(get(rs, i, "comname_str"));
			comsale.setSaletel(get(rs, i, "saletel"));
			comsale.setCreatetime(get(rs, i, "screatetime"));
			comsale.setRemark(get(rs, i, "remark"));
			comsale.setSalename(get(rs, i, "salename"));
			comsale.setSaleid(getInt(rs, i, "saleid"));
			comsale.setSalemail(get(rs, i, "salemail"));
			r.add(comsale);
		}
		return r;
	}
	public List get(String otherSQL, String orderField) {
		clearErr();
		DbConn conn = new DbConn();
		DbRs rs = null;
		try {
			String sql = "";
			sql = "select * from businesssales where 1=1";
			if (otherSQL != null && !otherSQL.trim().equals(""))
				sql += otherSQL;
			if (orderField != null && !orderField.trim().equals(""))
				sql += " order by " + orderField;
			if (getTopNum() > 0)
				sql += " limit " + getTopNum();
			conn.prepare(sql);
			rs = conn.executeQuery();
		} catch (Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		} finally {
			conn.close();
		}
		if (rs != null && rs.size() > 0) {
			List r = new ArrayList();
			for (int i = 0; i < rs.size(); i++) {
				Companysale comsale = new Companysale(get(rs, i, "saleid"));
				comsale.setComid(getInt(rs, i, "comid"));
				comsale.setComname_str(get(rs, i, "comname_str"));
				comsale.setSaletel(get(rs, i, "saletel"));
				comsale.setCreatetime(get(rs, i, "createtime"));
				comsale.setRemark(get(rs, i, "remark"));
				comsale.setSalename(get(rs, i, "salename"));
				comsale.setSaleid(getInt(rs, i, "saleid"));
				comsale.setSalemail(get(rs, i, "salemail"));
				r.add(comsale);
			}
			return r;
		}
		return null;
	}
}
